﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
using Spire.Doc.Fields;




namespace EvTest.SpireWord
{
    public partial class ToPdfs : Form
    {
        private string saveDir = @"D:\EvTest\File\";

        public ToPdfs()
        {
            InitializeComponent();
        }

        private void btn1_Click(object sender, EventArgs e)
        {

            OpenFileDialog odf = new OpenFileDialog();
            odf.Multiselect = true;
            odf.RestoreDirectory = true;
            odf.Title = "打开你所选取的文件";
            odf.Filter = "Word文档|*.doc;*.docx";

            if (odf.ShowDialog() == DialogResult.OK)
            {
                if (odf.FileNames.Length == 0)
                {
                    MessageBox.Show("您未选择任何文件");
                    return;
                }

                for (int i = 0; i < odf.FileNames.Length; i++)
                {
                    dgv.Rows.Add(odf.SafeFileNames[i], odf.FileNames[i]);
                }

            }
        }

        private void InsertTextWatermark(Section section)
        {
            TextWatermark txtWatermark = new TextWatermark();
            txtWatermark.Text = textBox1.Text;
            txtWatermark.FontSize = 95;
            txtWatermark.Color = Color.Blue;
            txtWatermark.Layout = WatermarkLayout.Diagonal;
            section.Document.Watermark = txtWatermark;

        }

        private void InsertHeaderAndFooter(Section section)
        {
            HeaderFooter header = section.HeadersFooters.Header;
            HeaderFooter footer = section.HeadersFooters.Footer;

            //insert picture and text to header
            Paragraph headerParagraph = header.AddParagraph();

            //header text
            TextRange text = headerParagraph.AppendText("Demo of Spire.Doc");
            text.CharacterFormat.FontName = "Arial";
            text.CharacterFormat.FontSize = 10;
            text.CharacterFormat.Italic = true;
            headerParagraph.Format.HorizontalAlignment
                = Spire.Doc.Documents.HorizontalAlignment.Right;

            //border
            headerParagraph.Format.Borders.Bottom.BorderType
                = Spire.Doc.Documents.BorderStyle.Single;
            headerParagraph.Format.Borders.Bottom.Space = 0.05F;


            //insert picture to footer
            Paragraph footerParagraph = footer.AddParagraph();

            //insert page number
            footerParagraph.AppendField("page number", FieldType.FieldPage);
            footerParagraph.AppendText(" of ");
            footerParagraph.AppendField("number of pages", FieldType.FieldNumPages);
            footerParagraph.Format.HorizontalAlignment
                = Spire.Doc.Documents.HorizontalAlignment.Right;

            //border
            footerParagraph.Format.Borders.Top.BorderType
                = Spire.Doc.Documents.BorderStyle.Single;
            footerParagraph.Format.Borders.Top.Space = 0.05F;
        }

        private void btn2_Click(object sender, EventArgs e)
        {

            if (dgv.Rows.Count == 0)
            {
                MessageBox.Show("您还没有选择文件");
                return;
            }


            // 1. 添加水印
            // 2. 添加密码
            // 3. 转换PDF文件
            foreach (DataGridViewRow R in dgv.Rows)
            {
                string filepath = R.Cells[1].Value.ToString().Trim();

                //Open a Word document as template.
                Document document = new Document(filepath);

                //Insert text watermark.
                InsertTextWatermark(document.Sections[0]);


                Section section = document.Sections[0];
                //insert header and footer
                InsertHeaderAndFooter(section);

                //Save as docx file.
                document.SaveToFile(saveDir + "Sample.docx", FileFormat.Docx);


                //create a parameter
                ToPdfParameterList toPdf = new ToPdfParameterList();

                //set the password
                string password = textBox2.Text.Trim();
                toPdf.PdfSecurity.Encrypt(password, password, Spire.Pdf.Security.PdfPermissionsFlags.Default, Spire.Pdf.Security.PdfEncryptionKeySize.Key128Bit);
                //save doc file.
                document.SaveToFile(saveDir + "EncryptWithPassword.pdf", toPdf);


                //Launching the MS Word file.
                System.Diagnostics.Process.Start(saveDir + "EncryptWithPassword.pdf");



            }

        }
    }
}
